<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>renderer_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_soy_renderer_test.js.html">renderer_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2011 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.soy.RendererTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.soy.RendererTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.dom&#39;);
<a name="line19"></a>goog.require(&#39;goog.dom.NodeType&#39;);
<a name="line20"></a>goog.require(&#39;goog.dom.TagName&#39;);
<a name="line21"></a>goog.require(&#39;goog.html.SafeHtml&#39;);
<a name="line22"></a>goog.require(&#39;goog.i18n.bidi.Dir&#39;);
<a name="line23"></a>goog.require(&#39;goog.soy.Renderer&#39;);
<a name="line24"></a>goog.require(&#39;goog.soy.data.SanitizedContentKind&#39;);
<a name="line25"></a>/** @suppress {extraRequire} */
<a name="line26"></a>goog.require(&#39;goog.soy.testHelper&#39;);
<a name="line27"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line28"></a>goog.require(&#39;goog.testing.recordFunction&#39;);
<a name="line29"></a>
<a name="line30"></a>var handleRender;
<a name="line31"></a>
<a name="line32"></a>
<a name="line33"></a>function setUp() {
<a name="line34"></a>  // Replace the empty default implementation.
<a name="line35"></a>  handleRender = goog.soy.Renderer.prototype.handleRender =
<a name="line36"></a>      goog.testing.recordFunction(goog.soy.Renderer.prototype.handleRender);
<a name="line37"></a>}
<a name="line38"></a>
<a name="line39"></a>
<a name="line40"></a>var dataSupplier = {
<a name="line41"></a>  getData: function() {
<a name="line42"></a>    return {name: &#39;IjValue&#39;};
<a name="line43"></a>  }
<a name="line44"></a>};
<a name="line45"></a>
<a name="line46"></a>
<a name="line47"></a>function testRenderElement() {
<a name="line48"></a>  var testDiv = goog.dom.createElement(goog.dom.TagName.DIV);
<a name="line49"></a>
<a name="line50"></a>  var renderer = new goog.soy.Renderer(dataSupplier);
<a name="line51"></a>  renderer.renderElement(
<a name="line52"></a>      testDiv, example.injectedDataTemplate, {name: &#39;Value&#39;});
<a name="line53"></a>  assertEquals(&#39;ValueIjValue&#39;, elementToInnerHtml(testDiv));
<a name="line54"></a>  assertEquals(testDiv, handleRender.getLastCall().getArguments()[0]);
<a name="line55"></a>  handleRender.assertCallCount(1);
<a name="line56"></a>}
<a name="line57"></a>
<a name="line58"></a>
<a name="line59"></a>function testRenderElementWithNoTemplateData() {
<a name="line60"></a>  var testDiv = goog.dom.createElement(goog.dom.TagName.DIV);
<a name="line61"></a>
<a name="line62"></a>  var renderer = new goog.soy.Renderer(dataSupplier);
<a name="line63"></a>  renderer.renderElement(testDiv, example.noDataTemplate);
<a name="line64"></a>  assertEquals(&#39;&lt;div&gt;Hello&lt;/div&gt;&#39;, elementToInnerHtml(testDiv));
<a name="line65"></a>  assertEquals(testDiv, handleRender.getLastCall().getArguments()[0]);
<a name="line66"></a>  handleRender.assertCallCount(1);
<a name="line67"></a>}
<a name="line68"></a>
<a name="line69"></a>
<a name="line70"></a>function testRenderAsFragment() {
<a name="line71"></a>  var renderer = new goog.soy.Renderer(dataSupplier);
<a name="line72"></a>  var fragment = renderer.renderAsFragment(
<a name="line73"></a>      example.injectedDataTemplate, {name: &#39;Value&#39;});
<a name="line74"></a>  assertEquals(&#39;ValueIjValue&#39;, fragmentToHtml(fragment));
<a name="line75"></a>  assertEquals(fragment, handleRender.getLastCall().getArguments()[0]);
<a name="line76"></a>  handleRender.assertCallCount(1);
<a name="line77"></a>}
<a name="line78"></a>
<a name="line79"></a>
<a name="line80"></a>function testRenderAsFragmentWithNoTemplateData() {
<a name="line81"></a>  var renderer = new goog.soy.Renderer(dataSupplier);
<a name="line82"></a>  var fragment = renderer.renderAsFragment(example.noDataTemplate);
<a name="line83"></a>  assertEquals(goog.dom.NodeType.ELEMENT, fragment.nodeType);
<a name="line84"></a>  assertEquals(&#39;&lt;div&gt;Hello&lt;/div&gt;&#39;, fragmentToHtml(fragment));
<a name="line85"></a>  assertEquals(fragment, handleRender.getLastCall().getArguments()[0]);
<a name="line86"></a>  handleRender.assertCallCount(1);
<a name="line87"></a>}
<a name="line88"></a>
<a name="line89"></a>
<a name="line90"></a>function testRenderAsElement() {
<a name="line91"></a>  var renderer = new goog.soy.Renderer(dataSupplier);
<a name="line92"></a>  var element = renderer.renderAsElement(
<a name="line93"></a>      example.injectedDataTemplate, {name: &#39;Value&#39;});
<a name="line94"></a>  assertEquals(&#39;ValueIjValue&#39;, elementToInnerHtml(element));
<a name="line95"></a>  assertEquals(element, handleRender.getLastCall().getArguments()[0]);
<a name="line96"></a>  handleRender.assertCallCount(1);
<a name="line97"></a>}
<a name="line98"></a>
<a name="line99"></a>
<a name="line100"></a>function testRenderAsElementWithNoTemplateData() {
<a name="line101"></a>  var renderer = new goog.soy.Renderer(dataSupplier);
<a name="line102"></a>  var elem = renderer.renderAsElement(example.noDataTemplate);
<a name="line103"></a>  assertEquals(&#39;Hello&#39;, elementToInnerHtml(elem));
<a name="line104"></a>  assertEquals(elem, handleRender.getLastCall().getArguments()[0]);
<a name="line105"></a>}
<a name="line106"></a>
<a name="line107"></a>
<a name="line108"></a>function testRenderConvertsToString() {
<a name="line109"></a>  var renderer = new goog.soy.Renderer(dataSupplier);
<a name="line110"></a>  assertEquals(&#39;Output should be a string&#39;,
<a name="line111"></a>      &#39;Hello &lt;b&gt;World&lt;/b&gt;&#39;, renderer.render(example.sanitizedHtmlTemplate));
<a name="line112"></a>  assertUndefined(handleRender.getLastCall().getArguments()[0]);
<a name="line113"></a>  handleRender.assertCallCount(1);
<a name="line114"></a>}
<a name="line115"></a>
<a name="line116"></a>
<a name="line117"></a>function testRenderRejectsNonHtmlStrictTemplates() {
<a name="line118"></a>  var renderer = new goog.soy.Renderer(dataSupplier);
<a name="line119"></a>  assertEquals(
<a name="line120"></a>      &#39;Assertion failed: &#39; +
<a name="line121"></a>      &#39;render was called with a strict template of kind other than &quot;html&quot;&#39; +
<a name="line122"></a>      &#39; (consider using renderText or renderStrict)&#39;,
<a name="line123"></a>      assertThrows(function() {
<a name="line124"></a>        renderer.render(example.unsanitizedTextTemplate, {});
<a name="line125"></a>      }).message);
<a name="line126"></a>  handleRender.assertCallCount(0);
<a name="line127"></a>}
<a name="line128"></a>
<a name="line129"></a>
<a name="line130"></a>function testRenderStrictDoesNotConvertToString() {
<a name="line131"></a>  var renderer = new goog.soy.Renderer(dataSupplier);
<a name="line132"></a>  var result = renderer.renderStrict(example.sanitizedHtmlTemplate);
<a name="line133"></a>  assertEquals(&#39;Hello &lt;b&gt;World&lt;/b&gt;&#39;, result.content);
<a name="line134"></a>  assertEquals(goog.soy.data.SanitizedContentKind.HTML, result.contentKind);
<a name="line135"></a>  assertUndefined(handleRender.getLastCall().getArguments()[0]);
<a name="line136"></a>  handleRender.assertCallCount(1);
<a name="line137"></a>}
<a name="line138"></a>
<a name="line139"></a>
<a name="line140"></a>function testRenderStrictValidatesOutput() {
<a name="line141"></a>  var renderer = new goog.soy.Renderer(dataSupplier);
<a name="line142"></a>  // Passes.
<a name="line143"></a>  renderer.renderStrict(example.sanitizedHtmlTemplate, {});
<a name="line144"></a>  // No SanitizedContent at all.
<a name="line145"></a>  assertEquals(
<a name="line146"></a>      &#39;Assertion failed: &#39; +
<a name="line147"></a>      &#39;renderStrict cannot be called on a non-strict soy template&#39;,
<a name="line148"></a>      assertThrows(function() {
<a name="line149"></a>        renderer.renderStrict(example.noDataTemplate, {});
<a name="line150"></a>      }).message);
<a name="line151"></a>  assertUndefined(handleRender.getLastCall().getArguments()[0]);
<a name="line152"></a>  // Passes.
<a name="line153"></a>  renderer.renderStrict(example.sanitizedHtmlTemplate, {},
<a name="line154"></a>      goog.soy.data.SanitizedContentKind.HTML);
<a name="line155"></a>  // Wrong content kind.
<a name="line156"></a>  assertEquals(
<a name="line157"></a>      &#39;Assertion failed: &#39; +
<a name="line158"></a>      &#39;renderStrict was called with the wrong kind of template&#39;,
<a name="line159"></a>      assertThrows(function() {
<a name="line160"></a>        renderer.renderStrict(example.sanitizedHtmlTemplate, {},
<a name="line161"></a>            goog.soy.data.SanitizedContentKind.JS);
<a name="line162"></a>      }).message);
<a name="line163"></a>  assertUndefined(handleRender.getLastCall().getArguments()[0]);
<a name="line164"></a>
<a name="line165"></a>  // renderStrict&#39;s opt_kind parameter defaults to SanitizedContentKind.HTML:
<a name="line166"></a>  // Passes.
<a name="line167"></a>  renderer.renderStrict(example.sanitizedHtmlTemplate, {});
<a name="line168"></a>  // Rendering non-HTML template fails:
<a name="line169"></a>  assertEquals(
<a name="line170"></a>      &#39;Assertion failed: &#39; +
<a name="line171"></a>      &#39;renderStrict was called with the wrong kind of template&#39;,
<a name="line172"></a>      assertThrows(function() {
<a name="line173"></a>        renderer.renderStrict(example.unsanitizedTextTemplate, {});
<a name="line174"></a>      }).message);
<a name="line175"></a>  assertUndefined(handleRender.getLastCall().getArguments()[0]);
<a name="line176"></a>  handleRender.assertCallCount(3);
<a name="line177"></a>}
<a name="line178"></a>
<a name="line179"></a>
<a name="line180"></a>function testRenderText() {
<a name="line181"></a>  var renderer = new goog.soy.Renderer(dataSupplier);
<a name="line182"></a>  // RenderText converts to string.
<a name="line183"></a>  assertEquals(&#39;Output of renderText should be a string&#39;,
<a name="line184"></a>      &#39;I &lt;3 Puppies &amp; Kittens&#39;,
<a name="line185"></a>      renderer.renderText(example.unsanitizedTextTemplate));
<a name="line186"></a>  assertUndefined(handleRender.getLastCall().getArguments()[0]);
<a name="line187"></a>  // RenderText on non-strict template fails.
<a name="line188"></a>  assertEquals(
<a name="line189"></a>      &#39;Assertion failed: &#39; +
<a name="line190"></a>      &#39;renderText cannot be called on a non-strict soy template&#39;,
<a name="line191"></a>      assertThrows(function() {
<a name="line192"></a>        renderer.renderText(example.noDataTemplate, {});
<a name="line193"></a>      }).message);
<a name="line194"></a>  // RenderText on non-text template fails.
<a name="line195"></a>  assertEquals(
<a name="line196"></a>      &#39;Assertion failed: &#39; +
<a name="line197"></a>      &#39;renderText was called with a template of kind other than &quot;text&quot;&#39;,
<a name="line198"></a>      assertThrows(function() {
<a name="line199"></a>        renderer.renderText(example.sanitizedHtmlTemplate, {});
<a name="line200"></a>      }).message);
<a name="line201"></a>  handleRender.assertCallCount(1);
<a name="line202"></a>}
<a name="line203"></a>
<a name="line204"></a>function testRenderSafeHtml() {
<a name="line205"></a>  var renderer = new goog.soy.Renderer(dataSupplier);
<a name="line206"></a>  var result = renderer.renderSafeHtml(example.sanitizedHtmlTemplate);
<a name="line207"></a>  assertEquals(&#39;Hello &lt;b&gt;World&lt;/b&gt;&#39;, goog.html.SafeHtml.unwrap(result));
<a name="line208"></a>  assertEquals(goog.i18n.bidi.Dir.LTR, result.getDirection());
<a name="line209"></a>}
</pre>


</body>
</html>
